package tools

import (
	"os"
	"os/signal"
	"time"
)

func ParseYearTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseMonthTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseDayTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseHourTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02T15Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02 15", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseMinuteTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02T15:04Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02 15:04", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseSecondTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02T15:04:05Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02 15:04:05", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseMillisecondTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02T15:04:05.000Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02 15:04:05.000", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseMicrosecondTime(stamp string) (time.Time, bool) {
	if t, err := time.ParseInLocation("2006-01-02T15:04:05.000000Z07:00", stamp, time.Local); err == nil {
		return t, true
	}
	if t, err := time.ParseInLocation("2006-01-02 15:04:05.000000", stamp, time.Local); err == nil {
		return t, true
	}
	return time.Time{}, false
}

func ParseAllTime(stamp string) (time.Time, bool) {
	if t, success := ParseMicrosecondTime(stamp); success {
		return t, true
	} else if t, success := ParseMillisecondTime(stamp); success {
		return t, true
	} else if t, success := ParseSecondTime(stamp); success {
		return t, true
	} else if t, success := ParseMinuteTime(stamp); success {
		return t, true
	} else if t, success := ParseHourTime(stamp); success {
		return t, true
	} else if t, success := ParseDayTime(stamp); success {
		return t, true
	} else if t, success := ParseMonthTime(stamp); success {
		return t, true
	} else if t, success := ParseYearTime(stamp); success {
		return t, true
	}
	return time.Time{}, false
}

func ToMinuteTimeStamp(t time.Time) string {
	return t.UTC().Local().Format("2006-01-02T15:04Z07:00")
}

// Sleep sleep一定时间
// 单位为秒
func Sleep(seconds float32) {
	var sleepSec = seconds * 1e9
	time.Sleep(time.Duration(sleepSec))
}

// Wait 提供阻塞的循环，用于保持线程
func Wait() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	<-c
}

//Interrupt 接收系统打断信号
func Interrupt() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	<-c
}

//Shutdown 接收系统关机信号
func Shutdown() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Kill)
	<-c
}

var ok = true

func init() {
	go func() {
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt, os.Kill)
		<-c
		ok = false
	}()
}

func Ok() bool {
	return ok
}
